package com.rado.tf.gui.view.userList.impl;

import com.rado.tf.domain.User;
import com.rado.tf.gui.event.UIEvent;
import com.rado.tf.gui.event.UIEventTypes;
import com.rado.tf.gui.view.common.View;
import com.rado.tf.gui.view.userList.IUserListPresenter;
import com.rado.tf.gui.view.userList.IUserListView;
import com.rado.tf.service.IUserService;
import com.vaadin.data.Property;
import com.vaadin.ui.Table;
import org.springframework.context.ApplicationEvent;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/**
 * Created by parado on 18.03.14.
 */
@Component(UserListPresenterImpl.BEAN_NAME)
public class UserListPresenterImpl implements IUserListPresenter
{
   public static final String BEAN_NAME = "userListPresenter";

   @Resource
   private IUserListView userListView;

   @Resource
   private IUserService userService;

   @Override
   public View getViewDisplay()
   {
      return userListView;
   }

   @Override
   @PostConstruct
   public void bind()
   {
      final Table table = userListView.getTable();
      table.addValueChangeListener(new Property.ValueChangeListener()
      {
         @Override
         public void valueChange(Property.ValueChangeEvent event)
         {
            userListView.editUser((User) table.getValue());
         }
      });
   }

   @Override
   public void onApplicationEvent(ApplicationEvent event)
   {
      if (event instanceof UIEvent)
      {
         if (UIEventTypes.MENU_USER_LIST_SELECTED == ((UIEvent) event).getEventType())
         {
            userListView.setUserList(userService.findAll());
         }
      }
   }
}
